﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.ComponentModel;
using System.Collections.Specialized;
using System.Collections;
using System.Web;
using Microsoft.SharePoint.Navigation;
using Microsoft.SharePoint.Publishing.Navigation;
using Microsoft.SharePoint.Publishing;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint.Utilities;

namespace LanguageConfigurationSolution.LanguageSwitch
{
    public class LanguageSwitch : LanguageSwitchBase
    {

        /// <summary>
        /// By default use the "CombinedNavSiteMapProvider"
        /// </summary>
        private PortalSiteMapProvider _Provider = PortalSiteMapProvider.CombinedNavSiteMapProvider;

        /// <summary>
        /// The provider which will be used
        /// </summary>
        public PortalSiteMapProvider Provider
        {
            get
            {
                return _Provider;
            }
            set
            {
                _Provider = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="node"></param>
        /// <param name="count"></param>
        private void CreateLanguageSwitch(SPSite site, PortalSiteMapNode node, ref int count)
        {
            using (SPWeb web = site.OpenWeb(node.WebId))
            {
                bool Supressed = false;

                // Only show SubWebs which should be considered Language SubWebs.
                if (!web.Properties.ContainsKey("LanguageSubWeb")) Supressed = true;

                foreach (object s in Suppress)
                {
                    #region Check if the current web is to be suppressed
                    if (s is SuppressWeb)
                    {
                        SuppressWeb sw = (SuppressWeb)s;
                        if (sw.Match(web))
                        {
                            Supressed = true;
                            break;
                        }
                    }
                    else if (s is HtmlGenericControl)
                    {
                        // This error will be thrown to help development
                        throw new Exception("Place runat=\"server\"");
                    }
                    #endregion
                }

                if (!Supressed)
                {
                    #region Instantiate the Header- or SeperatorTemplate
                    if (count == 0)
                    {
                        if (HeaderTemplate != null) HeaderTemplate.InstantiateIn(this);
                    }
                    if (count > 0)
                    {
                        if (SeperatorTemplate != null) SeperatorTemplate.InstantiateIn(this);
                    }
                    #endregion

                    PublishingWeb pweb = null;
                    if (PublishingWeb.IsPublishingWeb(web)) pweb = PublishingWeb.GetPublishingWeb(web);
                    
                    LanguageSwitchContainer container = new LanguageSwitchContainer(this, web, pweb);

                    #region Determine if the web is the current Web
                    bool iscurrent = false;
                    SPWeb cweb = SPContext.Current.Web;
                    while (cweb != null)
                    {
                        if (cweb.ID.Equals(web.ID)) iscurrent = true;
                        cweb = cweb.ParentWeb;
                    }
                    #endregion

                    #region Instantiate a template for each Language SubWeb
                    if (CurrentTemplate != null && iscurrent)
                    {
                        CurrentTemplate.InstantiateIn(container);
                    }
                    else
                    {
                        if (LanguageTemplate != null) LanguageTemplate.InstantiateIn(container);
                    }
                    #endregion

                    Controls.Add(container);

                    container.DataBind(); // I hope this will improve performance, but am not too sure if 

                    //this.DataBind(); // ToDo: i don't think this is the correct place to DataBind (performance)
                    // I suspect i placed this here to be able to have the properties of SubWeb be able to be used from the ITemplate before the SubWeb is disposed

                    count++; // Increment count of Language SubWebs added
                }
            }
        }

        protected override void CreateLanguageSwitches()
        {
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                int count = 0;

                // Find the RootNode for the Current SiteCollection
                SiteMapNode rootnode = Provider.FindSiteMapNode(site.ServerRelativeUrl);
                foreach (SiteMapNode node in rootnode.ChildNodes)
                {
                    if (node is PortalSiteMapNode) CreateLanguageSwitch(site, (PortalSiteMapNode)node, ref count);
                }

                if (count > 0)
                {
                    if (FooterTemplate != null) FooterTemplate.InstantiateIn(this);
                }
                else
                {
                    if (EmptyTemplate != null) EmptyTemplate.InstantiateIn(this);
                }
            }
        }

        


    }
}
